package com.xmad.ksadutils.AdSDK.AdMold.Pangolin;


import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
import com.bytedance.sdk.openadsdk.TTFullScreenVideoAd;
import com.xmad.ksadutils.AdSDK.AdBase.AdBaseLoad;
import com.xmad.ksadutils.AdSDK.AdBase.AdCode;
import com.xmad.ksadutils.AdSDK.AdBase.TTAdManagerHolder;
import com.xmad.ksadutils.AdSDK.Interface.AdLoad;
import com.xmad.ksadutils.AdSDK.Utlis.RxLogUtils;

public class PGloadFullScreenVideoAd extends AdBaseLoad implements AdLoad {
    private volatile static PGloadFullScreenVideoAd mInstance;
    private TTAdNative mTTAdNative;
    private TTFullScreenVideoAd mttFullVideoAd;
    private boolean mIsLoaded = false; //视频是否加载完成
    /**
     * 单例模式
     *
     * @return
     */
    public static PGloadFullScreenVideoAd getInstance() {
        if (mInstance == null) {
            synchronized (PGloadFullScreenVideoAd.class) {
                if (mInstance == null) {
                    mInstance = new PGloadFullScreenVideoAd();
                }
            }
        }
        return mInstance;
    }

    @Override
    public void Show(final Activity activity, FrameLayout frameLayout) {
        if (AdCode.getLoadPGLoadFullVideoId().equals("")&&PGAdId==null) {
            RxLogUtils.e("还未设置ID");
            return;
        }
        //step2:(可选，强烈建议在合适的时机调用):申请部分权限，如read_phone_state,防止获取不了imei时候，下载类广告没有填充的问题。
        //  TTAdManagerHolder.get().requestPermissionIfNecessary(context);
        //step3:创建TTAdNative对象,用于调用广告请求接口
        mTTAdNative= TTAdManagerHolder.get().createAdNative(activity);
        //step4:创建广告请求参数AdSlot,具体参数含义参考文档
        if (ToastCode){
            Toast.makeText(activity,!PGAdId.equals("") ? PGAdId: AdCode.getLoadPGLoadFullVideoId(), Toast.LENGTH_SHORT).show();
        }
        AdSlot adSlot = new AdSlot.Builder()
                    .setCodeId(!PGAdId.equals("") ? PGAdId: AdCode.getLoadPGLoadFullVideoId())
                    .build();
        //step5:请求广告
        mTTAdNative.loadFullScreenVideoAd(adSlot, new TTAdNative.FullScreenVideoAdListener(){
            @Override
            public void onError(int code, String msg) {
                Log.d(TAG, "Callback --> onError: " + code + ", " + String.valueOf(msg));
                if (listener!=null){
                    listener.onError(code+msg);
                }
            }

            @Override
            public void onFullScreenVideoAdLoad(final TTFullScreenVideoAd ad) {
                Log.d(TAG, "Callback --> onFullScreenVideoAdLoad");
                mttFullVideoAd = ad;
                mttFullVideoAd.setFullScreenVideoAdInteractionListener(new TTFullScreenVideoAd.FullScreenVideoAdInteractionListener() {

                            @Override
                            public void onAdShow() {
                               RxLogUtils.d("onAdShow");
                                if (listener!=null){
                                    listener.onAdShow();
                                }
                            }

                            @Override
                            public void onAdVideoBarClick() {
                                RxLogUtils.d("onAdVideoBarClick");
                                if (listener!=null){
                                    listener.onAdClicked();
                                }
                            }

                            @Override
                            public void onAdClose() {
                                RxLogUtils.d("onAdClose");
                                if (listener!=null){
                                    listener.close();
                                }
                            }

                            @Override
                            public void onVideoComplete() {
                                RxLogUtils.d("onVideoComplete");
                                if (listener!=null){
                                    listener.onComplete();
                                }
                            }

                            @Override
                            public void onSkippedVideo() {
                                RxLogUtils.d("onSkippedVideo");
                            }

                        });
                mIsLoaded = false;
                if (mttFullVideoAd != null) {
                    //展示广告，并传入广告展示的场景
                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                        @Override
                        public void run() {
                            mttFullVideoAd.showFullScreenVideoAd(activity);
                        }
                    });
                } else {
                    RxLogUtils.e("请先加载广告");
                }

                ad.setDownloadListener(new TTAppDownloadListener() {
                    @Override
                    public void onIdle() {
                    }

                    @Override
                    public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
                        Log.d("DML", "onDownloadActive==totalBytes=" + totalBytes + ",currBytes=" + currBytes + ",fileName=" + fileName + ",appName=" + appName);


                    }

                    @Override
                    public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
                        Log.d("DML", "onDownloadPaused===totalBytes=" + totalBytes + ",currBytes=" + currBytes + ",fileName=" + fileName + ",appName=" + appName);

                    }

                    @Override
                    public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
                        Log.d("DML", "onDownloadFailed==totalBytes=" + totalBytes + ",currBytes=" + currBytes + ",fileName=" + fileName + ",appName=" + appName);

                    }

                    @Override
                    public void onDownloadFinished(long totalBytes, String fileName, String appName) {
                        Log.d("DML", "onDownloadFinished==totalBytes=" + totalBytes + ",fileName=" + fileName + ",appName=" + appName);

                    }

                    @Override
                    public void onInstalled(String fileName, String appName) {
                        Log.d("DML", "onInstalled==" + ",fileName=" + fileName + ",appName=" + appName);

                    }
                });
            }

            @Override
            public void onFullScreenVideoCached() {
                Log.d(TAG, "Callback --> onFullScreenVideoCached");
                mIsLoaded = true;
            }

            @Override
            public void onFullScreenVideoCached(TTFullScreenVideoAd ttFullScreenVideoAd) {
                Log.d(TAG, "Callback --> onFullScreenVideoCached");
                mIsLoaded = true;
            }
        });

    }




}
